package ThreadTest_p.p1_counterr.v2;

import ThreadTest_p.p1_counterr.v1.Counter;

public class CounterTestV2 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        // 创建多个线程，它们都使用同一个计数器
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                counter.add();
                System.out.println(Thread.currentThread().getName() + ": " + counter.count);
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                counter.add();
                System.out.println(Thread.currentThread().getName() + ": " + counter.count);
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();


    }
}
